package com.infinit.easyreader.Biz;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.infinit.easyreader.entity.RankContent;
import com.infinit.easyreader.utils.Consts;
import com.infinit.easyreader.utils.NetUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类：com.infinit.easyreader.Biz.RankContentBizV[1.0.0]
 * 作用：获取排序的内容
 * Created by rzy on 2016/6/29.
 */
public class RankContentBiz implements Consts {
    public static final int CODE_RANKCONTENT_SUCCESS = 104;
    public static final int CODE_RANKCONTENT_FAIL = 105;
    private Map<String, String> params;
    private Handler handler;
    private int count = 10;
    private NetUtils netUtils;
    private Context context;
    private int start = 0;

    public RankContentBiz(Handler handler, Context context) {
        this.handler = handler;
        netUtils = NetUtils.getInstance();
        this.context = context;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public void setStart(int start) {
        this.start = start;
    }

    /**
     * 获取指定URL下的数据
     */

    public void getRankContentFromInternet() {
        String contentUrl = URL_RECOMMEND;
        contentUrl += "&contentType=" + CONTENTTYPE_ID;
        if (params != null) {
            contentUrl += "&";
            contentUrl += netUtils.urlencode(params);
        }
        contentUrl += "&count=" + count;
        contentUrl += "&start=" + start;
        //开始连接网络
        Log.d(TAG, "getRankContentFromInternet: " + contentUrl);
        StringRequest request = new StringRequest(contentUrl, new InnerListener(), new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Message.obtain(handler, CODE_RANKCONTENT_FAIL).sendToTarget();
            }
        });
        netUtils.addRequest(request, context);
    }


    private class InnerListener implements Response.Listener<String> {

        @Override
        public void onResponse(String response) {
//            Log.d(TAG, "onResponse:请求数据返回 " + response);
            try {
                JSONObject object = new JSONObject(response);
                if (object.getInt("error_code") != 0) {
                    Message.obtain(handler, CODE_RANKCONTENT_FAIL).sendToTarget();
                    return;
                }
                //解析数据

                JSONArray contentArray = object.getJSONObject("result").getJSONObject("GetSpecifiedRankRsp").getJSONObject("RankContentList").getJSONArray("RankContent");
                Gson mGson = new Gson();
                List<RankContent> contents = new ArrayList<>();
                for (int i = 0; i < contentArray.length(); i++) {
                    RankContent content = mGson.fromJson(contentArray.getString(i), RankContent.class);
                    contents.add(content);
                }
                Message.obtain(handler, CODE_RANKCONTENT_SUCCESS, contents).sendToTarget();

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    }


}
